Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports ASPNET.StarterKit.BusinessLogicLayer

Partial Public Class TimeEntry_aspx
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If (Not Page.IsPostBack) Then
            WeekEnding2.Text = DateTime.Now.ToString("MM/dd/yyyy")
            If (Page.User.IsInRole("ProjectAdministrator") OrElse Page.User.IsInRole("ProjectManager")) Then
                UserList.DataSourceID = "ProjectMembers"

                If Page.User.IsInRole("ProjectAdministrator") Then
                    ProjectData.SortParameterName = "sortParameter"
                    ProjectData.SelectMethod = "GetAllProjects"
                ElseIf Page.User.IsInRole("ProjectManager") Then

                    ProjectData.SelectParameters.Add(New Parameter("userName", TypeCode.String, Page.User.Identity.Name))
                    ProjectData.SortParameterName = "sortParameter"
                    ProjectData.SelectMethod = "GetProjectsByManagerUserName"
                End If

            Else

                ProjectData.SelectParameters.Add(New Parameter("userName", TypeCode.String, Page.User.Identity.Name))
                ProjectData.SelectMethod = "GetProjectsByUserName"

                UserList.Items.Add(Page.User.Identity.Name)

            End If

            ProjectList.DataBind()
            UserList.DataBind()
            ProjectListGridView.DataBind()

            'Set Default user
            UserList.SelectedIndex = UserList.Items.IndexOf(UserList.Items.FindByText(Page.User.Identity.Name))

            If ProjectList.Items.Count >= 1 Then
                TimeEntryView.Visible = True
                MessageView.Visible = False
            Else
                TimeEntryView.Visible = False
                MessageView.Visible = True
            End If
        End If

        ' Check for Null Reference which occurs on first install before users and projects are configured
        If UserList.SelectedItem IsNot Nothing Then
            If UserList.SelectedItem.Value <> Page.User.Identity.Name Then
                AddEntry.Enabled = False
            Else
                AddEntry.Enabled = True
            End If
        End If

        If UserList.Items.Count = 0 Then
            AddEntry.Enabled = False
        End If

    End Sub

    Protected Sub AddEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddEntry.Click

        Dim timeEntry As TimeEntry = New TimeEntry(Page.User.Identity.Name, Convert.ToInt32(CategoryList.SelectedValue), Convert.ToDecimal(Hours.Text), WeekEnding2.Text, UserList.SelectedValue)
        timeEntry.Description = Description.Text
        timeEntry.Save()

        Description.Text = String.Empty
        Hours.Text = String.Empty
        ProjectListGridView.DataBind()

    End Sub

    Protected Sub Cancel_Click(ByVal sender As Object, ByVal args As EventArgs) Handles Cancel.Click

        Description.Text = String.Empty
        Hours.Text = String.Empty

    End Sub

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate

        If Description.Text.Length > 200 Then
            args.IsValid = False
        Else
            args.IsValid = True
        End If

    End Sub

    'Hides and Shows the AddHours Panel when editing Previous Time Entries

    Protected Sub ShowAddHours(ByVal sender As Object, ByVal args As EventArgs)
        pnlAddHours.Visible = True

    End Sub

    Protected Sub HideAddHours(ByVal sender As Object, ByVal args As EventArgs)
        pnlAddHours.Visible = False

    End Sub
End Class

